Load Prompts from Github Repo and auto populate n8n expressions
工作流概述
这是一个包含17个节点的复杂工作流,主要用于自动化处理各种任务。
工作流源代码
{
"id": "QyMyf3zraY0wxXDf",
"meta": {
"instanceId": "ba3fa76a571c35110ef5f67e5099c9a5c1768ef125c2f3b804ba20de75248c0b",
"templateCredsSetupCompleted": true
},
"name": "Load Prompts from Github Repo and auto populate n8n expressions",
"tags": [],
"nodes": [
{
"id": "34781446-b06e-41eb-83b8-b96bda1a5595",
"name": "When clicking ‘Test workflow’",
"type": "n8n-nodes-base.manualTrigger",
"position": [
-80,
0
],
"parameters": {},
"typeVersion": 1
},
{
"id": "c53b7243-7c82-47e0-a5ee-bd82bc51c386",
"name": "GitHub",
"type": "n8n-nodes-base.github",
"position": [
600,
0
],
"parameters": {
"owner": {
"__rl": true,
"mode": "name",
"value": "={{ $json.Account }}"
},
"filePath": "={{ $json.path }}{{ $json.prompt }}",
"resource": "file",
"operation": "get",
"repository": {
"__rl": true,
"mode": "name",
"value": "={{ $json.repo }}"
},
"additionalParameters": {}
},
"credentials": {
"githubApi": {
"id": "ostHZNoe8GSsbaQM",
"name": "The GitHub account"
}
},
"typeVersion": 1
},
{
"id": "9976b199-b744-47a7-9d75-4b831274c01b",
"name": "Extract from File",
"type": "n8n-nodes-base.extractFromFile",
"position": [
840,
0
],
"parameters": {
"options": {},
"operation": "text"
},
"typeVersion": 1
},
{
"id": "26aa4e6a-c487-4cdf-91d5-df660cf826a6",
"name": "setVars",
"type": "n8n-nodes-base.set",
"position": [
180,
0
],
"parameters": {
"options": {},
"assignments": {
"assignments": [
{
"id": "150618c5-09b1-4f8b-a7b4-984662bf3381",
"name": "Account",
"type": "string",
"value": "TPGLLC-US"
},
{
"id": "22e8a3b0-bd53-485c-b971-7f1dd0686f0e",
"name": "repo",
"type": "string",
"value": "PeresPrompts"
},
{
"id": "ab94d0a1-ef3a-4fe9-9076-6882c6fda0ac",
"name": "path",
"type": "string",
"value": "SEO/"
},
{
"id": "66f122eb-1cbd-4769-aac8-3f05cdb6c116",
"name": "prompt",
"type": "string",
"value": "keyword_research.md"
},
{
"id": "03fe26a3-04e6-439c-abcb-d438fc5203c0",
"name": "company",
"type": "string",
"value": "South Nassau Physical Therapy"
},
{
"id": "c133d216-a457-4872-a060-0ba4d94549af",
"name": "product",
"type": "string",
"value": "Manual Therapy"
},
{
"id": "584864dd-2518-45e2-b501-02828757fc3a",
"name": "features",
"type": "string",
"value": "pain relief"
},
{
"id": "0c4594cc-302a-4215-bdad-12cf54f57967",
"name": "sector",
"type": "string",
"value": "physical therapy"
}
]
}
},
"typeVersion": 3.4
},
{
"id": "9d92f581-8cd9-448c-aa1d-023a96c1ddda",
"name": "replace variables",
"type": "n8n-nodes-base.code",
"position": [
1900,
-20
],
"parameters": {
"jsCode": "// Fetch the prompt text
const prompt = $('SetPrompt').first().json.data; // Ensure the prompt contains placeholders like {{ some.node.value }}
// Example variables object
const variables = {
company: $('setVars').first().json.company,
features: \"Awesome Software\",
keyword: \"2025-02-07\"
};
// Function to replace placeholders dynamically
const replaceVariables = (text, vars) => {
return text.replace(/{{(.*?)}}/g, (match, key) => {
const trimmedKey = key.trim();
// Extract last part after the last dot
const finalKey = trimmedKey.split('.').pop();
// Replace if key exists, otherwise leave placeholder unchanged
return vars.hasOwnProperty(finalKey) ? vars[finalKey] : match;
});
};
// Replace and return result
return [{
prompt: replaceVariables(prompt, variables)
}];
"
},
"typeVersion": 2
},
{
"id": "6c6c4fde-6ee5-47a8-894d-44d1afcedc2a",
"name": "If",
"type": "n8n-nodes-base.if",
"position": [
1560,
0
],
"parameters": {
"options": {},
"conditions": {
"options": {
"version": 2,
"leftValue": "",
"caseSensitive": true,
"typeValidation": "strict"
},
"combinator": "and",
"conditions": [
{
"id": "2717a7e5-095a-42bf-8b5b-8050c3389ec5",
"operator": {
"type": "boolean",
"operation": "true",
"singleValue": true
},
"leftValue": "={{ $json.success }}",
"rightValue": "={{ $('Check All Prompt Vars Present').item.json.keys()}}"
}
]
}
},
"typeVersion": 2.2
},
{
"id": "3b7712b8-5152-4f60-9401-03c89c39e227",
"name": "Check All Prompt Vars Present",
"type": "n8n-nodes-base.code",
"position": [
1280,
0
],
"parameters": {
"jsCode": "// Get prompt text
const prompt = $json.data;
// Extract variables inside {{ }} dynamically
const matches = [...prompt.matchAll(/{{(.*?)}}/g)];
const uniqueVars = [...new Set(matches.map(match => match[1].trim().split('.').pop()))];
// Get variables from the Set Node
const setNodeVariables = $node[\"setVars\"].json || {};
// Log extracted variables and Set Node keys
console.log(\"Extracted Variables:\", uniqueVars);
console.log(\"Set Node Keys:\", Object.keys(setNodeVariables));
// Check if all required variables are present in the Set Node
const missingKeys = uniqueVars.filter(varName => !setNodeVariables.hasOwnProperty(varName));
console.log(\"Missing Keys:\", missingKeys);
// Return false if any required variable is missing, otherwise return true
return [{
success: missingKeys.length === 0,
missingKeys: missingKeys
}];
"
},
"typeVersion": 2
},
{
"id": "32618e10-3285-4c16-9e78-058dde329337",
"name": "SetPrompt",
"type": "n8n-nodes-base.set",
"position": [
1060,
0
],
"parameters": {
"options": {},
"assignments": {
"assignments": [
{
"id": "335b450d-542a-4714-83d8-ccc237188fc5",
"name": "data",
"type": "string",
"value": "={{ $json.data }}"
}
]
}
},
"typeVersion": 3.4
},
{
"id": "4d8b34ca-50dd-4f37-b4f7-542291461662",
"name": "Stop and Error",
"type": "n8n-nodes-base.stopAndError",
"position": [
1900,
200
],
"parameters": {
"errorMessage": "=Missing Prompt Variables : {{ $('Check All Prompt Vars Present').item.json.missingKeys }}
"
},
"typeVersion": 1
},
{
"id": "a78c1e17-9152-4241-bcdf-c0d723da543b",
"name": "Set Completed Prompt",
"type": "n8n-nodes-base.set",
"position": [
2220,
-20
],
"parameters": {
"options": {},
"assignments": {
"assignments": [
{
"id": "57a9625b-adea-4ee7-a72a-2be8db15f3d4",
"name": "Prompt",
"type": "string",
"value": "={{ $json.prompt }}"
}
]
}
},
"typeVersion": 3.4
},
{
"id": "51447c90-a222-4172-a49b-86ec43332559",
"name": "AI Agent",
"type": "@n8n/n8n-nodes-langchain.agent",
"position": [
2440,
-20
],
"parameters": {
"text": "={{ $json.Prompt }}",
"options": {},
"promptType": "define"
},
"typeVersion": 1.7
},
{
"id": "f15b6af1-7af2-4515-be8f-960211118dce",
"name": "Sticky Note",
"type": "n8n-nodes-base.stickyNote",
"position": [
60,
-120
],
"parameters": {
"width": 340,
"height": 260,
"content": "# Set The variables in your prompt here"
},
"typeVersion": 1
},
{
"id": "163db6cc-5b06-4ae6-ac97-5890b37cdb18",
"name": "Sticky Note1",
"type": "n8n-nodes-base.stickyNote",
"position": [
520,
-120
],
"parameters": {
"color": 5,
"content": "## The repo is currently public for you to test with"
},
"typeVersion": 1
},
{
"id": "83ff6a86-a759-42a9-ace4-e20d57b906db",
"name": "Sticky Note2",
"type": "n8n-nodes-base.stickyNote",
"position": [
1780,
-200
],
"parameters": {
"width": 360,
"height": 260,
"content": "## Replaces the values in the prompt with the variables in the
# 'setVars' Node"
},
"typeVersion": 1
},
{
"id": "7dd61153-84ac-4b59-b449-333825476c33",
"name": "Sticky Note3",
"type": "n8n-nodes-base.stickyNote",
"position": [
2000,
180
],
"parameters": {
"color": 3,
"content": "## If you're missing variables they will be listed here"
},
"typeVersion": 1
},
{
"id": "1f070dc3-3d25-41d8-b534-912ba7c8b2b0",
"name": "Prompt Output",
"type": "n8n-nodes-base.set",
"position": [
2800,
-20
],
"parameters": {
"options": {},
"assignments": {
"assignments": [
{
"id": "01a30683-c348-4712-a3b1-739fc4a17718",
"name": "promptResponse",
"type": "string",
"value": "={{ $json.output }}"
}
]
}
},
"typeVersion": 3.4
},
{
"id": "2d12a6e2-7976-41b0-8cb2-01466b28269d",
"name": "Ollama Chat Model",
"type": "@n8n/n8n-nodes-langchain.lmChatOllama",
"position": [
2480,
200
],
"parameters": {
"options": {}
},
"credentials": {
"ollamaApi": {
"id": "ERfZ8mAfQ1b0aoxZ",
"name": "Ollama account"
}
},
"typeVersion": 1
}
],
"active": false,
"pinData": {},
"settings": {
"executionOrder": "v1"
},
"versionId": "4327a337-59e7-4b5b-98e8-93c6be550972",
"connections": {
"If": {
"main": [
[
{
"node": "replace variables",
"type": "main",
"index": 0
}
],
[
{
"node": "Stop and Error",
"type": "main",
"index": 0
}
]
]
},
"GitHub": {
"main": [
[
{
"node": "Extract from File",
"type": "main",
"index": 0
}
]
]
},
"setVars": {
"main": [
[
{
"node": "GitHub",
"type": "main",
"index": 0
}
]
]
},
"AI Agent": {
"main": [
[
{
"node": "Prompt Output",
"type": "main",
"index": 0
}
]
]
},
"SetPrompt": {
"main": [
[
{
"node": "Check All Prompt Vars Present",
"type": "main",
"index": 0
}
]
]
},
"Extract from File": {
"main": [
[
{
"node": "SetPrompt",
"type": "main",
"index": 0
}
]
]
},
"Ollama Chat Model": {
"ai_languageModel": [
[
{
"node": "AI Agent",
"type": "ai_languageModel",
"index": 0
}
]
]
},
"replace variables": {
"main": [
[
{
"node": "Set Completed Prompt",
"type": "main",
"index": 0
}
]
]
},
"Set Completed Prompt": {
"main": [
[
{
"node": "AI Agent",
"type": "main",
"index": 0
}
]
]
},
"Check All Prompt Vars Present": {
"main": [
[
{
"node": "If",
"type": "main",
"index": 0
}
]
]
},
"When clicking ‘Test workflow’": {
"main": [
[
{
"node": "setVars",
"type": "main",
"index": 0
}
]
]
}
}
}
功能特点
- 自动检测新邮件
- AI智能内容分析
- 自定义分类规则
- 批量处理能力
- 详细的处理日志
技术分析
节点类型及作用
- Manualtrigger
- Github
- Extractfromfile
- Set
- Code
复杂度评估
配置难度:
维护难度:
扩展性:
实施指南
前置条件
- 有效的Gmail账户
- n8n平台访问权限
- Google API凭证
- AI分类服务订阅
配置步骤
- 在n8n中导入工作流JSON文件
- 配置Gmail节点的认证信息
- 设置AI分类器的API密钥
- 自定义分类规则和标签映射
- 测试工作流执行
- 配置定时触发器(可选)
关键参数
| 参数名称 | 默认值 | 说明 |
|---|---|---|
| maxEmails | 50 | 单次处理的最大邮件数量 |
| confidenceThreshold | 0.8 | 分类置信度阈值 |
| autoLabel | true | 是否自动添加标签 |
最佳实践
优化建议
- 定期更新AI分类模型以提高准确性
- 根据邮件量调整处理批次大小
- 设置合理的分类置信度阈值
- 定期清理过期的分类规则
安全注意事项
- 妥善保管API密钥和认证信息
- 限制工作流的访问权限
- 定期审查处理日志
- 启用双因素认证保护Gmail账户
性能优化
- 使用增量处理减少重复工作
- 缓存频繁访问的数据
- 并行处理多个邮件分类任务
- 监控系统资源使用情况
故障排除
常见问题
邮件未被正确分类
检查AI分类器的置信度阈值设置,适当降低阈值或更新训练数据。
Gmail认证失败
确认Google API凭证有效且具有正确的权限范围,重新进行OAuth授权。
调试技巧
- 启用详细日志记录查看每个步骤的执行情况
- 使用测试邮件验证分类逻辑
- 检查网络连接和API服务状态
- 逐步执行工作流定位问题节点
错误处理
工作流包含以下错误处理机制:
- 网络超时自动重试(最多3次)
- API错误记录和告警
- 处理失败邮件的隔离机制
- 异常情况下的回滚操作